package com.pkdl.dao;

import com.pkdl.dto.HoSoBenhAnDTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

// Data access object, which get data of HoSoBenhAn table from database
public class HoSoBenhAnDAO {
	public ArrayList<HoSoBenhAnDTO> selectAll() {
		ArrayList<HoSoBenhAnDTO> list = new ArrayList<HoSoBenhAnDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_HoSoBenhAn_SelectAll()}");
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				HoSoBenhAnDTO hosobenhan = new HoSoBenhAnDTO();
				hosobenhan.setMaBenhAn(rs.getInt("MaBenhAn"));
				hosobenhan.setMaBenhNhan(rs.getInt("MaBenhNhan"));
				hosobenhan.setMaBenhHoc(rs.getInt("MaBenhHoc"));
				hosobenhan.setNguoiLapBenhAn(rs.getInt("NguoiLapBenhAn"));
				hosobenhan.setNgayLapBenhAn(rs.getDate("NgayLapBenhAn"));
				list.add(hosobenhan);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public HoSoBenhAnDTO selectById(int mabenhan) {
		HoSoBenhAnDTO hosobenhan = new HoSoBenhAnDTO();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_HoSoBenhAn_SelectByID(?)}");
			cs.setInt(1, mabenhan);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				hosobenhan.setMaBenhAn(rs.getInt("MaBenhAn"));
				hosobenhan.setMaBenhNhan(rs.getInt("MaBenhNhan"));
				hosobenhan.setMaBenhHoc(rs.getInt("MaBenhHoc"));
				hosobenhan.setNguoiLapBenhAn(rs.getInt("NguoiLapBenhAn"));
				hosobenhan.setNgayLapBenhAn(rs.getDate("NgayLapBenhAn"));
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return hosobenhan;
	}

	public boolean insert(HoSoBenhAnDTO hosobenhan) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_HoSoBenhAn_Insert(?, ?, ?, ?)}");
			cs.setInt(1, hosobenhan.getMaBenhNhan());
			cs.setInt(2, hosobenhan.getMaBenhHoc());
			cs.setInt(3, hosobenhan.getNguoiLapBenhAn());
			cs.setDate(4, new java.sql.Date(hosobenhan.getNgayLapBenhAn().getTime()));
			int n = cs.executeUpdate();
			if (n == 0) {
				result =false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean update(HoSoBenhAnDTO hosobenhan) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_HoSoBenhAn_Update(?, ?, ?, ?, ?)}");
			cs.setInt(1, hosobenhan.getMaBenhAn());
			cs.setInt(2, hosobenhan.getMaBenhNhan());
			cs.setInt(3, hosobenhan.getMaBenhHoc());
			cs.setInt(4, hosobenhan.getNguoiLapBenhAn());
			cs.setDate(5, new java.sql.Date(hosobenhan.getNgayLapBenhAn().getTime()));
			int n = cs.executeUpdate();
			if (n == 0) {
				result =false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean delete(int mabenhan) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_HoSoBenhAn_Delete(?)}");
			cs.setInt(1, mabenhan);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public ArrayList<HoSoBenhAnDTO> selectByMaBenhNhan(int mabenhnhan) {
		ArrayList<HoSoBenhAnDTO> list = new ArrayList<HoSoBenhAnDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_HoSoBenhAn_SelectByMaBenhNhan(?)}");
			cs.setInt(1, mabenhnhan);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				HoSoBenhAnDTO hosobenhan = new HoSoBenhAnDTO();
				hosobenhan.setMaBenhAn(rs.getInt("MaBenhAn"));
				hosobenhan.setMaBenhNhan(rs.getInt("MaBenhNhan"));
				hosobenhan.setMaBenhHoc(rs.getInt("MaBenhHoc"));
				hosobenhan.setNguoiLapBenhAn(rs.getInt("NguoiLapBenhAn"));
				hosobenhan.setNgayLapBenhAn(rs.getDate("NgayLapBenhAn"));
				list.add(hosobenhan);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public ArrayList<HoSoBenhAnDTO> selectByMaBenhHoc(int mabenhhoc) {
		ArrayList<HoSoBenhAnDTO> list = new ArrayList<HoSoBenhAnDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_HoSoBenhAn_SelectByMaBenhHoc(?)}");
			cs.setInt(1, mabenhhoc);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				HoSoBenhAnDTO hosobenhan = new HoSoBenhAnDTO();
				hosobenhan.setMaBenhAn(rs.getInt("MaBenhAn"));
				hosobenhan.setMaBenhNhan(rs.getInt("MaBenhNhan"));
				hosobenhan.setMaBenhHoc(rs.getInt("MaBenhHoc"));
				hosobenhan.setNguoiLapBenhAn(rs.getInt("NguoiLapBenhAn"));
				hosobenhan.setNgayLapBenhAn(rs.getDate("NgayLapBenhAn"));
				list.add(hosobenhan);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public ArrayList<HoSoBenhAnDTO> selectByNguoiLapBenhAn(int nguoilapbenhan) {
		ArrayList<HoSoBenhAnDTO> list = new ArrayList<HoSoBenhAnDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_HoSoBenhAn_SelectByNguoiLapBenhAn(?)}");
			cs.setInt(1, nguoilapbenhan);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				HoSoBenhAnDTO hosobenhan = new HoSoBenhAnDTO();
				hosobenhan.setMaBenhAn(rs.getInt("MaBenhAn"));
				hosobenhan.setMaBenhNhan(rs.getInt("MaBenhNhan"));
				hosobenhan.setMaBenhHoc(rs.getInt("MaBenhHoc"));
				hosobenhan.setNguoiLapBenhAn(rs.getInt("NguoiLapBenhAn"));
				hosobenhan.setNgayLapBenhAn(rs.getDate("NgayLapBenhAn"));
				list.add(hosobenhan);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(HoSoBenhAnDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return list;
	}

}
